// 路由主文件
import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */

// 另外建立一个数组， 专门放可以动态变动的页面
import departmentsRouter from './modules(路由模块)/departments'
import employeesRouter from './modules(路由模块)/employees'
import settingRouter from './modules(路由模块)/setting'
import salarysRouter from './modules(路由模块)/salarys'
import socialRouter from './modules(路由模块)/social'
import attendancesRouter from './modules(路由模块)/attendances'
import approvalsRouter from './modules(路由模块)/approvals'
import permissionRouter from './modules(路由模块)/permission'

// 动态路由
// 动态路由是随着权限不一样， 能看的路由也是有区别的，
// 静态路由是全部人都能看到的路由
export const asyncRoutes = [
  departmentsRouter,
  employeesRouter,
  settingRouter,
  salarysRouter,
  attendancesRouter,
  approvalsRouter,
  permissionRouter,
  socialRouter
]

export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/test',
    component: () => import('@/views/test')
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      // path 加斜杆代表从根路径访问
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '首页', icon: 'dashboard' }
    }]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  // router 是新键路由实例时， 路由表配置
  // 这里不仅仅要静态路由， 现在临时也将动态的合并在一起， 先让所有所有人都能看见， 方便开发
  // routes: constantRoutes
  routes: [...constantRoutes, ...asyncRoutes]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
